
package com.wenmang.networklib.frame;

import com.google.gson.Gson;
import com.wenmang.networklib.utils.LogUtils;

import java.io.UnsupportedEncodingException;

/**
 * desc:
 * 1.网络请求栈
 * 2.关联IHttpService与IHttpListener
 * <p>
 * author: created by zhoujx on 2018/10/19 13:59
 */
public class WMHttpTask<T> implements Runnable {
    private static final String TAG = WMHttpTask.class.getSimpleName();
    private IHttpService httpService;
    private IHttpListener httpListener;

    public WMHttpTask(T requestParmas, String url, IHttpService httpService, IHttpListener httpListener) {
        this.httpService = httpService;
        this.httpListener = httpListener;
        this.httpService.setUrl(url);
        this.httpService.setHttpListener(this.httpListener);

        if (null != requestParmas) {
            String requestContent = new Gson().toJson(requestParmas);
            try {
                this.httpService.setParmas(requestContent.getBytes("utf-8"));
            } catch (UnsupportedEncodingException e) {
                LogUtils.e(TAG, e.getMessage());
            }
        }
    }

    @Override
    public void run() {
        this.httpService.execute();
    }
}
